﻿using KPI.Api;
using KPI.Api.Apis;
using KPI.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPF_TY_UI;

namespace KPIClient.Views
{
    /// <summary>
    /// RecordDetail.xaml 的交互逻辑
    /// </summary>
    public partial class RecordDetail : TYUserControl
    {
        ViewModelRecordDetail vm = new ViewModelRecordDetail();
        private readonly string examId;

        public RecordDetail(string examId)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewQuestionList_Loaded;
            this.examId = examId;
        }

        private void ViewQuestionList_Loaded(object sender, RoutedEventArgs e)
        {
            BtnClick_Search(null, null);
        }

        private async void BtnClick_Search(object sender, RoutedEventArgs e)
        {
            pager.PageIndex = 1;
            await QueryData();
        }


        private async void Pager_IndexChanged(object sender, RoutedEventArgs e)
        {
            //去查询数据
            await QueryData();
        }

        async Task<bool> QueryData()
        {
            QuestionBankQueryDto query = new QuestionBankQueryDto();
            query.page = pager.PageIndex;
            query.limit = pager.PageSize;

            query.questionName = vm.Name;


            //if (vm.SelectedQtType != null && vm.SelectedQtType.Key >= 0)
            //{
            //    query.queryType = vm.SelectedQtType.Key;
            //}
            //if (vm.DtStart != null)
            //{
            //    query.startDate = vm.DtStart;
            //}
            //if (vm.DtEnd != null)
            //{
            //    query.endDate = vm.DtEnd;
            //}

            vm.ListUserInfoInExam.Clear();
            var rData = await Run(() => QuesionNaireApi.GetQuesionNaireUserList(examId, ""));


            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return false;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return false;
            }
            if (rData.data != null)
            {
                foreach (var item in rData.data?.list)
                {
                    ExamUserInfo model = new ExamUserInfo();
                    model.FromData(item);
                    model.Index = vm.ListUserInfoInExam.Count + 1;
                    vm.ListUserInfoInExam.Add(model);
                }
                pager.DataCount = rData.data.total;
                pager.PageCount = rData.data.pages;
                vm.SelectedUserInfoInExam = vm.ListUserInfoInExam.FirstOrDefault();
                return true;
            }
            return true;
        }

        private void BtnClick_UserDetail(object sender, RoutedEventArgs e)
        {

        }

        private async void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (vm.SelectedUserInfoInExam == null)
                return;
            vm.ListQt.Clear();
            var examId = vm.SelectedUserInfoInExam.Data.examId;
            var userId = vm.SelectedUserInfoInExam.Data.userId;
            if (examId == null)
            {
                MessageBox.Show("examId为空!");
                return;
            }

            if (userId == null)
            {
                MessageBox.Show("userId为空!");
                return;
            }
            var rData = await Run(() => QuesionNaireApi.GetNaireDetailByUserId(examId, userId));


            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            foreach (var item in rData.data.listHistoryQuestion)
            {
                QuestionInfoModel questionInfoModel = new QuestionInfoModel();
                questionInfoModel.FromData(item);


                questionInfoModel.Index = vm.ListQt.Count + 1;
                vm.ListQt.Add(questionInfoModel);
            }
        }
    }
}

